// pages/recruit/singleRecruit/singleRecruit.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    resume_id: '',
    anchor_id: '',
    recruit_id: '',
    resume_message_id: '',
    resumes: [],
    stay: false,
    role: '',

    // 找主播弹框开始

    flag_find_anchor_nojob: true,
    flag_find_anchor_call: true,
    flag_find_anchor_stay: true,
    flag_find_anchor_call_no: true,
    flag_find_anchor_stay_no: true,
    flag_find_anchor_jobing: true,
    flag_find_anchor_call_bodou_no: true,
    flag_find_anchor_call_bodou: true,
  },
  // 次数
  getcount() {
    wx.request({
      url: app.globalData.url + 'api/swop/guild/number',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: (e) => {
        let count = e.data.data.count,
          all_total = e.data.data.all_total;
        // console.log('count=' + count)
        this.setData({
          count: count,
          all_total: all_total,
        })
      },
    })
  },
  talk_close: function () {
    this.setData({
      qiang: false
    })
  },
  //抢主播支付波豆
  payBdQZB(resume_id, type, active_id, price) {
    wx.request({
      url: app.globalData.url + 'api/resume/guild/grab/anchor/order',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        type: type,
        active_id: active_id,
        price: price
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.setData({
          qiang: false
        })
        var chat_id = e.data.data.chat_id;
        wx.navigateTo({
          url: '../session/index?chat_id=' + chat_id + '&title=直播之家'
        })
      },
    })
  },
  //点击抢主播
  clickQzb(e) {
    console.log(e)
    let active_id = e.currentTarget.dataset.active_id ? e.currentTarget.dataset.active_id : "",
      is_activity = e.currentTarget.dataset.is_activity ? e.currentTarget.dataset.is_activity : "",
      talk_resume_id = e.currentTarget.dataset.talk_resume_id ? e.currentTarget.dataset.talk_resume_id : "",
      usable_bd = e.currentTarget.dataset.usable_bd ? parseInt(e.currentTarget.dataset.usable_bd) : "",
      xf_bd = e.currentTarget.dataset.xf_bd ? parseInt(e.currentTarget.dataset.xf_bd) : "",
      g_price = e.currentTarget.dataset.g_price ? parseInt(e.currentTarget.dataset.g_price) : "",
      activityinfo = e.currentTarget.dataset.activityinfo,
      activity = e.currentTarget.dataset.activity;
    this.setData({
      qiang: false
    })
    if (is_activity != "") {
      //排除活动状态为空
      if (is_activity == 1) {
        //活动状态
        if (usable_bd > g_price) {
          this.payBdQZB(talk_resume_id, is_activity, active_id, g_price);
        } else {
          this.setData({
            flag_find_anchor_call_bodou_no: false
          })
        }
      } else {
        //非活动状态
        if (usable_bd > xf_bd) {
          this.payBdQZB(talk_resume_id, is_activity, 0, xf_bd);
        } else {
          this.setData({
            flag_find_anchor_call_bodou_no: false
          })
        }
      }
    } else {
      console.log("活动错误")
    }
  },
  getQZB(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/resume/guild/grab/anchor',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let activityInfo = e.data.data,
          activity = e.data.data.activity;
        this.setData({
          activityInfo: activityInfo,
          activity: activity
        })
      },
    })
  },
  //去约聊
  click_session(e) {
    console.log(e);
    let resume_id = e.currentTarget.dataset.resume_id,
      is_issue = this.data.is_issue,
      residue_num = this.data.residue_num,
      nickname = e.currentTarget.dataset.nickname,
      is_chat = e.currentTarget.dataset.is_chat,
      is_rob = e.currentTarget.dataset.is_rob;
    this.getQZB(resume_id);
    if (this.data.is_issue == 0) {
      //未发布
      this.setData({
        flag_find_anchor_nojob: false,
      })
    } else if (is_issue == "2" && residue_num == "0") {
      this.setData({
        flag_find_anchor_jobing: false,
      })
    } else {

      //修改这边 20190218
      if (is_rob == 1) {
        this.session(resume_id, nickname);
      } else {
        this.setData({
          qiang: true
        })
        // console.log(e)
        let nickname = e.currentTarget.dataset.nickname,
          type_name = e.currentTarget.dataset.type_name;
        this.setData({
          talk_nickname: nickname,
          talk_type_name: type_name,
          talk_resume_id: resume_id,
        })
      }
    }
  },
  session(resume_id, nickname) {
    wx.request({
      url: app.globalData.url + 'api/guild/chat/anchor',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        type: 1,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        var chat_id = e.data.data.chat_id;
        wx.navigateTo({
          url: '../session/index?chat_id=' + chat_id + '&title=' + nickname
        })
        // other.chat_id = e.data.data.chat_id
      }
    })
  },
  //去充值
  go_recharge() {
    wx.navigateTo({
      url: '../../payment/bodou/index',
    })
  },
  //点击主播收藏
  click_anchor_col(e) {
    let that = this;
    let resume_id = e.currentTarget.dataset.resume_id;
    let index = e.currentTarget.dataset.resumesindex;
    let resumes = this.data.resumes;

    if (resumes.is_collect == 1) {
      //已收藏
      wx.showToast({
        title: '取消收藏成功',
        icon: 'none',
      })
      resumes.is_collect = "0";
      this.setData({
        resumes: resumes,
      })
      this.anchor_col_no(resume_id);
    } else {
      //未收藏
      wx.showToast({
        title: '收藏成功',
        icon: 'none',
      })
      resumes.is_collect = "1";
      this.setData({
        resumes: resumes,
      })
      this.anchor_col(resume_id);
    }

    console.log(e)
  },
  //收藏操作
  anchor_col(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/resume/do/collect',
      data: {
        token: app.globalData.token,
        resume_id: resume_id
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },
    })
  },
  //取消收藏操作
  anchor_col_no(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/resume/cancel/collect',
      data: {
        token: app.globalData.token,
        resume_id: resume_id
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },
    })
  },
  //加入已沟通
  get_link(anchor_id, resume_id, resume_message_id, recruit_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/link/up/anchor',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        anchor_id: anchor_id,
        recruit_id: recruit_id,
        resume_message_id: resume_message_id,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },
    })
  },
  //获得详情
  get_resumes(resume_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/timing/resume/detail',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        let resume = e.data.data.resume;
        that.setData({
          resumes: resume,
        })
        // console.log(resume)
      },
    })
  },
  //剩余发布次数
  residue_time() {
    wx.request({
      url: app.globalData.url + 'api/recruit/limit/number',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        let last_time = e.data.data.is_send;
        this.setData({
          last_time: last_time,
        })
      },
    })
  },
  //点击
  click_frelease() {
    this.residue_time();
    var that = this;
    this.setData({
      flag_float: true,
      flag_find_anchor_nojob: true,
    });
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token
      },
      method: 'POST',
      success: function(e) {
        if (e.data.data.company.is_perfect <= 0) {
          wx.navigateTo({
            url: '../../mine/CompanyBasis/CompanyBasis?ProcessJudge=false',
          })
        } else {
          if (that.data.last_time <= 0) {
            wx.showModal({
              content: '职位发布数量已用完,暂不能发布新的职位',
              showCancel: false,
              confirmText: '好的',
              confirmColor: '#d0a76c',
            })
          } else {
            wx.navigateTo({
              url: '../postJob/postJob?home=true',
            })
          }

        }
      }
    })
  },
  //打电话、留电话
  click_anchor_call(e) {
    console.log(e)
    let that = this;
    let phone = this.data.resumes.telephone;
    var bd = this.data.register_all.bd, resumes = this.data.resumes;
    console.log('phone+' + this.data.resumes.telephone)
    if (resumes.is_sign==2){
      //未签约
      if (this.data.col) {
        //收藏入口
        if (this.data.resumes.is_linkup == 1) {
          //已沟通
          let is_issue = this.data.is_issue,
            residue_num = this.data.residue_num,
            re_number = this.data.re_number;
          if (is_issue == "2" && residue_num == "0") {
            that.setData({
              flag_find_anchor_jobing: false,
            })
          } else {
            if (phone) {
              //拨电话
              wx.makePhoneCall({
                phoneNumber: phone,
              })
            } else {
              //留电话
              wx.showToast({
                title: '您已留过电话',
                icon: 'none'
              })
            }
          }
        } else {
          //未沟通
          let is_issue = this.data.is_issue,
            residue_num = this.data.residue_num,
            re_number = this.data.re_number;
          if (is_issue == "0") {
            //没发布过
            that.setData({
              flag_find_anchor_nojob: false,
            })
            this.get_residue_time(1);
            this.get_is_issue();
            console.log(1)
          } else if (is_issue == "2" && residue_num == "0") {
            that.setData({
              flag_find_anchor_jobing: false,
            })
          } else {
            if (phone) {
              if (residue_num == "0") {
                if (bd >= 30) {
                  that.setData({
                    flag_find_anchor_call_bodou: false,
                  })
                } else {
                  that.setData({
                    flag_find_anchor_call_bodou_no: false,
                  })
                }
                // that.setData({
                //   flag_find_anchor_call_no: false,
                // })
                this.get_residue_time(1);
                this.get_is_issue();
                // console.log(2)
              } else {
                that.setData({
                  flag_find_anchor_call: false,
                })
                this.get_residue_time(1);
                this.get_is_issue();
                console.log(3)
              }
            } else {
              if (re_number > 0) {
                that.setData({
                  flag_find_anchor_stay: false,
                })
                this.get_residue_time(1);
                this.get_is_issue();
                console.log(5)
              } else {
                that.setData({
                  flag_find_anchor_stay_no: false,
                })
                this.get_residue_time(1);
                this.get_is_issue();
                console.log(4)

              }
            }
          }
        }
      } else {
        //其他入口
        if (that.data.chat_id) {
          that.callphone(that.data.chat_id)
        } else {
          console.log('不是chat_id')
        }
        let is_issue = this.data.is_issue,
          residue_num = this.data.residue_num,
          re_number = this.data.re_number;
        if (is_issue == "0") {
          //没发布过
          that.setData({
            flag_find_anchor_nojob: false,
          })
          this.get_residue_time(1);
          this.get_is_issue();
          console.log(1)
        } else if (is_issue == "2" && residue_num == "0") {
          that.setData({
            flag_find_anchor_jobing: false,
          })
        } else {
          wx.makePhoneCall({
            phoneNumber: phone,
          })
        }
      }
    }else{
      //已签约
      wx.showToast({
        title: '该主播已签约公会',
        icon: 'none'
      })
    }
    


  },
  //拨打电话
  callphone(chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/dial/telephone/resume',
      data: {
        token: app.globalData.token,
        chat_id: chat_id
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      }
    })
  },
  //拨打次数
  callTime() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },
    })
  },
  //点击减少找主播 播豆电话次数
  reduce_callbd(e) {
    let resume_id = e.currentTarget.dataset.resume_id,
      is_linkup = e.currentTarget.dataset.is_linkup,
      telephone = e.currentTarget.dataset.telephone,
      resumesindex = e.currentTarget.dataset.resumesindex,
      resumes = this.data.resumes;
    if (is_linkup == "1") {
      wx.makePhoneCall({
        phoneNumber: telephone,
        success(e) {
          // console.log(e.errMsg == 'makePhoneCall:ok')
        }
      })
      this.setData({
        flag_find_anchor_call_bodou: true,
      })
    } else {
      this.reduseBd(resume_id);
      this.link_up_anchor(0, resume_id);
      this.get_residue_time(1);
      this.get_phone();
      this.get_is_issue();
      wx.makePhoneCall({
        phoneNumber: telephone,
        success(e) {
          // console.log(e.errMsg =='makePhoneCall:ok')
        }
      })
      this.setData({
        flag_find_anchor_call_bodou: true,
      })
    }

    // resumes[resumesindex].is_linkup = 1;
    // this.setData({
    //   resumes: resumes
    // })
  },
  //点击减少找主播 的电话次数
  reduce_callnum(e) {
    // console.log(e)
    let resume_id = e.currentTarget.dataset.resume_id,
      is_linkup = e.currentTarget.dataset.is_linkup,
      telephone = e.currentTarget.dataset.telephone,
      // resumesindex = e.currentTarget.dataset.resumesindex,
      resumes = this.data.resumes;
    if (is_linkup == "1") {
      wx.makePhoneCall({
        phoneNumber: telephone,
        success(e) {}
      })
    } else {
      this.residue(resume_id);
      this.link_up_anchor(0, resume_id)
      this.get_residue_time(1);
      this.get_is_issue();
      wx.makePhoneCall({
        phoneNumber: telephone,
        success(e) {}
      })
    }
    resumes.is_linkup = 1;
    this.setData({
      resumes: resumes
    })
  },
  residue(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/recruit/buy/residue/number',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
      },

    })
  },
  //扣除播豆
  reduseBd(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/recruit/buy/residue/number',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        bd_num: 30,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      },
    })
  },
  //会长主动沟通主播
  link_up_anchor(anchor_id, resume_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/driving/link/up/anchor',
      data: {
        token: app.globalData.token,
        anchor_id: anchor_id,
        resume_id: resume_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        let resumes = this.data.resumes;
        resumes.is_linkup = 1;
        this.setData({
          resumes: resumes,
        })
      },
    })
  },
  //查看剩余次数 && 留电话
  get_residue_time(is_leave, resume_id, telephone, bd_num) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/guild/leave/anchor',
      data: {
        token: app.globalData.token,
        is_leave: is_leave,
        resume_id: resume_id,
        telephone: telephone,
        bd_num: bd_num,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let re_number = e.data.data.re_number;
        that.setData({
          re_number: re_number,
        })
        console.log(that.data.re_number)
      },
    })
  },
  //加载是否 发布过
  get_is_issue() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token,
        type: "2",
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let is_issue = e.data.data.company.is_issue;
        let residue_num = e.data.data.company.residue_num,
          hf_bd2 = e.data.data.company.hf_bd2,
          hf_bd = e.data.data.company.hf_bd;
        that.setData({
          is_issue: is_issue,
          residue_num: residue_num,
          hf_bd2: hf_bd2,
          hf_bd: hf_bd,
        })
      },
    })
  },
  //输入预留手机号
  inp_find_anchor(e) {
    // console.log(e.detail.value)
    this.setData({
      after_telephone: e.detail.value,
    })
  },
  //默认手机号
  get_phone() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        console.log(e.data.data.register_info.telephone)
        let register_all = e.data.data;
        let telephone = e.data.data.register_info.telephone;
        that.setData({
          register_all: register_all,
          register_info: e.data.data.register_info,
          reg_telephone: telephone
        })
      },
    })

  },
  //模块框 Start
  modal_hide: function() {
    this.setData({
      flag_find_anchor_nojob: true,
      flag_find_anchor_call: true,
      flag_find_anchor_stay: true,
      flag_find_anchor_call_no: true,
      flag_find_anchor_stay_no: true,
      flag_find_anchor_jobing: true,
      flag_find_anchor_call_bodou_no: true,
      flag_find_anchor_call_bodou: true,
    })
  },
  //模块框 End

  //点击 弹窗留电话
  click_stay: function() {
    let that = this;
    if (that.data.is_resume == 0) {
      that.setData({
        more_show: true,
        show_modalWrap: true,
        flag_out: false,
      })
    } else {
      that.setData({
        more_show: true,
        flag_phone: false,
        show_modalWrap: true,
      })
    }
  },
  // 是否有简历
  pull_resume: function() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/link/up/list',
      method: 'POST',
      data: {
        token: app.globalData.token,
      },
      success: function(e) {
        // console.log(e.data.data.is_resume)
        that.setData({
          is_resume: e.data.data.is_resume
        })
      }
    })
  },
  //去购买
  click_buy() {
    this.setData({
      flag_find_anchor_call_no: true,
    })
    wx.navigateTo({
      url: '../../payment/index/index',
    })
  },

  //stay_anchor
  stay_anchor(e) {
    let that = this;
    console.log(e)

    let resume_id = e.currentTarget.dataset.resume_id,
      is_linkup = e.currentTarget.dataset.is_linkup,
      resumesindex = e.currentTarget.dataset.resumesindex,
      resumes = this.data.resumes,
      bodou = e.currentTarget.dataset.bodou,
      register_all = this.data.register_all,
      hf_bd2 = parseInt(this.data.hf_bd2),
      hf_bd = parseInt(this.data.hf_bd);
    let phone = this.data.after_telephone ? this.data.after_telephone : this.data.reg_telephone;
    if (is_linkup == "1") {
      that.setData({
        flag_find_anchor_stay: true,
        flag_find_anchor_stay_no: true,
      })
      wx.showToast({
        title: '您已对该主播留过电话',
        icon: 'none',
      })
    } else {
      if (bodou) {
        if ((parseInt(register_all.bd) < hf_bd2) || (parseInt(register_all.bd) < hf_bd)) {
          this.setData({
            flag_find_anchor_call_bodou_no: false,
            flag_find_anchor_stay: true,
            flag_find_anchor_stay_no: true,
          })
        } else {
          // console.log('resume_id=' + resume_id + 'phone=' + phone + 'hf_bd2=' + hf_bd2)
          this.get_residue_time(2, resume_id, phone, hf_bd2);
          this.get_residue_time(1);
          this.get_is_issue();
          resumes.is_linkup = 1;
          that.setData({
            flag_find_anchor_stay: true,
            resumes: resumes,
          })
          wx.showToast({
            title: '操作成功~',
            icon: 'none',
          })
        }

      } else {
        if (re_number > 0) {
          //有 留电话次数
          this.get_residue_time(2, resume_id, phone);
          this.get_residue_time(1);
          resumes[resumesindex].is_linkup = 1;
          that.setData({
            flag_find_anchor_stay: true,
            flag_find_anchor_stay_no: true,
            resumes: resumes,
          })
          wx.showToast({
            title: '操作成功~',
            icon: 'none',
          })
        } else {
          //没有 留电话次数
          this.setData({
            flag_find_anchor_call_bodou_no: false,
            flag_find_anchor_stay: true,
          })
        }
      }





      // this.get_residue_time(2, resume_id, phone);
      // this.get_residue_time(1);
      // resumes.is_linkup = 1;
      // that.setData({
      //   flag_find_anchor_stay: true,
      //   resumes: resumes,
      // })

      // wx.showToast({
      //   title: '操作成功~',
      //   icon: 'none',
      // })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    console.log(options)
    that.setData({
      role: app.register_info.register_role
    })
    if (options.flag) {
      that.setData({
        col: true,
      })
    }
    if (options.stay) {
      that.setData({
        stay: true,
      })
    }
    if (options.chat_id) {
      that.setData({
        chat_id: options.chat_id,
      })
    }
    let resume_id = options.resume_id ? options.resume_id : "",
      anchor_id = options.anchor_id ? options.anchor_id : "",
      recruit_id = options.recruit_id ? options.recruit_id : "",
      resume_message_id = options.resume_message_id ? options.resume_message_id : "";
    this.setData({
      resume_id: resume_id,
      anchor_id: anchor_id,
      recruit_id: recruit_id,
      resume_message_id: resume_message_id,

    })
    this.get_resumes(resume_id);

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getcount();
    this.get_is_issue();
    this.get_residue_time(1);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.get_residue_time(1);
    this.get_is_issue();
    this.get_phone();
    this.pull_resume();
    this.getcount();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})